//
//  CollisionManager.cpp
//  EidoTron
//
//  Created by Luca Di Franco on 27/06/11.
//  Copyright 2011 Luca Di Franco. All rights reserved.
//

#include "CollisionManager.h"

#include "Collider.h"
#include "SceneNode.h"

vector< Collider* >* CollisionManager::colliders = new vector< Collider* >();

vector< pair< SceneNode*, SceneNode* > >* CollisionManager::collisions = new vector< pair< SceneNode*, SceneNode* > >();

void CollisionManager::add(Collider* collider)
{
	colliders->push_back(collider);
}

void CollisionManager::remove(Collider* collider)
{
	vector< Collider* >::iterator iter;
	for (iter = colliders->begin(); (iter != colliders->end()) && ((*iter) != collider); iter++);
	if (iter != colliders->end())
		colliders->erase(iter);
}

void CollisionManager::checkCollisions()
{
	vector< Collider* >::iterator iter, test;
	for (iter = colliders->begin(); iter != colliders->end(); iter++)
		(*iter)->collide = false;
	collisions->clear();
	for (iter = colliders->begin(); iter != colliders->end(); iter++)
	{
		for (test = iter + 1; test != colliders->end(); test++)
		{
			if ((*iter)->checkCollision(*test))
			{
				(*iter)->collide = (*test)->collide = true;
				pair< SceneNode*, SceneNode* > collision;
				collision.first = (*iter)->relatedNode;
				collision.second = (*test)->relatedNode;
				collisions->push_back(collision);
			}
		}
	}
}

vector< pair<SceneNode*, SceneNode*> >* CollisionManager::getCollisions()
{
	return collisions;
}